use super::*;

#[derive(Debug, Clone, Copy)]
pub struct Table<'a> {
	// uint16	version	Version number (0 or 1).
	pub version: u16,
	pub num_recs: u16, //Number of VDMX groups present
	// uint16	numRatios	Number of aspect ratio groupings
	// RatioRange	ratRange[numRatios]	Ratio record array.
	pub rat_ranges: LazyArray16<'a, RatioRange>,
	// Offset16	vdmxGroupOffsets[numRatios]	Offsets from start of this table to the VDMXGroup table for a corresponding RatioRange record.
	pub vdmx_group: LazyOffsetArray16<'a, VDMXGroup<'a>>,
}
impl<'a> FromSlice<'a> for Table<'a> {
	fn parse(data: &'a [u8]) -> Option<Self> {
		let mut s = Stream::new(data);
		let version: u16 = s.read()?;
		let num_recs: u16 = s.read()?;
		let num_ratios: u16 = s.read()?;
		Some(Self {
			version,
			num_recs,
			rat_ranges: s.read_array16(num_ratios)?,
			vdmx_group: LazyOffsetArray16::new(data, s.read_array16(num_ratios)?),
		})
	}
}

#[derive(Debug, Clone, Copy)]
pub struct RatioRange {
	// uint8	bCharSet	Character set (see below).
	pub b_char_set: u8,
	// uint8	xRatio	Value to use for x-Ratio
	pub x_ratio: u8,
	// uint8	yStartRatio	Starting y-Ratio value.
	pub y_start_ratio: u8,
	// uint8	yEndRatio	Ending y-Ratio value.
	pub y_end_ratio: u8,
}
impl FromData for RatioRange {
	const SIZE: usize = 4;
	fn parse(data: &[u8]) -> Option<Self> {
		let mut s = Stream::new(data);
		Some(Self {
			b_char_set: s.read()?,
			x_ratio: s.read()?,
			y_start_ratio: s.read()?,
			y_end_ratio: s.read()?,
		})
	}
}

#[derive(Debug, Clone, Copy)]
pub struct VDMXGroup<'a> {
	// uint16	recs	Number of height records in this group
	// uint8	startsz	Starting yPelHeight
	pub startsz: u8,
	// uint8	endsz	Ending yPelHeight
	pub endsz: u8,
	// vTable	entry[recs]	The VDMX records
	pub entry: LazyArray16<'a, VTable>,
}
impl<'a> FromSlice<'a> for VDMXGroup<'a> {
	fn parse(data: &'a [u8]) -> Option<Self> {
		let mut s = Stream::new(data);
		let recs: u16 = s.read()?;
		Some(Self {
			startsz: s.read()?,
			endsz: s.read()?,
			entry: s.read_array16(recs)?,
		})
	}
}

#[derive(Debug, Clone, Copy)]
pub struct VTable {
	// uint16	yPelHeight	yPelHeight to which values apply.
	pub y_pel_height: u16,
	// int16	yMax	Maximum value (in pels) for this yPelHeight.
	pub y_max: i16,
	// int16	yMin	Minimum value (in pels) for this yPelHeight.
	pub y_min: i16,
}
impl FromData for VTable {
	const SIZE: usize = 6;
	fn parse(data: &[u8]) -> Option<Self> {
		let mut s = Stream::new(data);
		Some(Self {
			y_pel_height: s.read()?,
			y_max: s.read()?,
			y_min: s.read()?,
		})
	}
}
